// utils/mixins/websocketMixin.js
export default {
  created() {
    this.setupWebSocketListeners();
  },
  destroyed() {
    this.removeWebSocketListeners();
  },
  methods: {
    setupWebSocketListeners() {
      this.handlePrivate = (data) => {
        uni.showToast({ title: `私聊消息：用户 ${data.userId}` });
        this.onPrivateMessage(data);
      };
      uni.$on('websocket-private', this.handlePrivate);

      this.handleGroup = (data) => {
        uni.showToast({ title: `群 ${data.groupId} 收到消息` });
        this.onGroupMessage(data);
      };
      uni.$on('websocket-group', this.handleGroup);

      this.handleInvite = (data) => {
        uni.showToast({ title: `收到进群邀请：群 ${data.groupId}` });
        this.onInviteMessage(data);
      };
      uni.$on('websocket-invite', this.handleInvite);

      this.handleAddFriend = (data) => {
        uni.showToast({ title: `用户 ${data.userId} 请求加你为好友` });
        this.onAddFriendMessage(data);
      };
      uni.$on('websocket-add-friend', this.handleAddFriend);
    },
    removeWebSocketListeners() {
      uni.$off('websocket-private', this.handlePrivate);
      uni.$off('websocket-group', this.handleGroup);
      uni.$off('websocket-invite', this.handleInvite);
      uni.$off('websocket-add-friend', this.handleAddFriend);
    },
    // 子类可重写以下方法
    onPrivateMessage(data) {},
    onGroupMessage(data) {},
    onInviteMessage(data) {},
    onAddFriendMessage(data) {}
  }
};